package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.SysRoleMapper;
import com.jlkf.pojo.SysRole;
import com.jlkf.service.SysRoleService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

@Service
public class SysRoleServiceImpl implements SysRoleService{

	@Autowired
	private SysRoleMapper rm;
	
	@Override
	public ReturnData getList(Integer pageNumber, Integer pageSize, String name) {
		Map<String,Object> map = new HashMap<>();
		if(!StringTools.isEmpty(name)){
			map.put("sysRoleName", name);
		}
		try {
			PageHelper.startPage(pageNumber, pageSize);
			List<SysRole> list=rm.getList(map);
			 PageInfo<SysRole> info = new PageInfo<>(list);
			long totle= info.getTotal();
			return new ReturnData(list,info.getPages(),(int)totle,200,"成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public ReturnData addOrUpdateRole(String json) {
		SysRole r = StringTools.json2Obj(json, SysRole.class);
		if(r==null){
			return new ReturnData(201,"List must be filled");
		}
		try {
			if(r.getSysRoleId()!=null){
				int num=rm.updateByPrimaryKeySelective(r);
				if(num==0){
					return new ReturnData(202,"更新失败");
				}
				return new ReturnData(200,"更新成功");
			}else{
				r.setCreateTime(new Date());
				int num=rm.insertSelective(r);
				if(num==0){
					return new ReturnData(202,"保存失败");
				}
				return new ReturnData(200,"保存成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public ReturnData deleteRole(Long id) {
	try {
			int num=rm.deleteByPrimaryKey(id);
			if(num==0){
				return new ReturnData(202,"删除失败");
			}
			return new ReturnData(200,"删除成功");
	} catch (Exception e) {
		e.printStackTrace();
		return new ReturnData(500,"Wow, that's wrong");
	}
	}

}
